package com.woniuxy.car.commodity.service.core.controller;


import com.woniuxy.car.commodity.service.core.dto.CommodityDto;
import com.woniuxy.car.commodity.service.core.entity.Commodity;
import com.woniuxy.car.commodity.service.core.service.ICommodityService;
import com.woniuxy.common.api.CommonResult;
import com.woniuxy.common.api.enums.ResultCode;
import com.woniuxy.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author awz
 * @since 2022-03-01
 */
@Api(tags = "商品接口")
@AllArgsConstructor
@RestController
@RequestMapping("/commodity/commodity")
public class CommodityController {

    private ICommodityService commodityService;

    @ApiOperation("根据条件查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commodityDto", value = "商品信息dto对象")
    })
    @PostMapping("/showCommodities")
    public CommonResult<List<Commodity>> showCommodities(@RequestBody CommodityDto commodityDto){

        List<Commodity> commodities = null;
        try {
            commodities = commodityService.showCommodities(commodityDto);
        } catch (BusinessException e) {
            return CommonResult.failure(ResultCode.FAILED.getCode(), e.getMessage());
        }
        return CommonResult.success(commodities);
    }

    @ApiOperation("删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id")
    })
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> deleteCommodity(@PathVariable int id){
        commodityService.deleteCommodity(id);
        return CommonResult.success(true);
    }

    @ApiOperation("根据商品id查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id")
    })
    @GetMapping("/{id}")
    public CommonResult<Commodity> findCommodityById(@PathVariable int id){
        Commodity commodity = commodityService.findCommodityById(id);
        return CommonResult.success(commodity);
    }
    @ApiOperation("添加或修改商品，用于测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commodity", value = "商品对象")
    })
    @PostMapping("/saveOrUpdateCommodity")
    public CommonResult<Boolean> saveOrUpdateCommodity(@RequestBody Commodity commodity){
        commodityService.saveOrUpdate(commodity);
        return CommonResult.success(true);

    }
    @ApiOperation("添加商品")
    @ApiImplicitParam(name = "commodity", value = "商品对象")
    @PostMapping("/addCommodity")
    public CommonResult<Boolean> addCommodity(@RequestBody CommodityDto commodityDto){
        try {
            commodityService.addCommodity(commodityDto);
        } catch (BusinessException e) {
            return CommonResult.failure(ResultCode.DATA_IS_WRONG.getCode(), e.getMessage());
        }
        return CommonResult.success(true);
    }

    @ApiOperation("商品上架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id")
    })
        @PutMapping("/grounding/{id}")
    public CommonResult<Boolean> commodityGrounding(@PathVariable int id){
        try {
            commodityService.groundingOrDown(id, 1);
        } catch (BusinessException e) {
            return CommonResult.failure(ResultCode.FAILED.getCode(), e.getMessage());
        }
        return CommonResult.success(true);
    }

    @ApiOperation("商品下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id")
    })
    @PutMapping("/down/{id}")
    public CommonResult<Boolean> commodityDown(@PathVariable int id){

        try {
            commodityService.groundingOrDown(id, 0);
        } catch (BusinessException e) {
            return CommonResult.failure(ResultCode.FAILED.getCode(), e.getMessage());
        }
        return CommonResult.success(true);
    }

    @ApiOperation("批量上架商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "商品id集合")
    })
    @PutMapping("/batchGrounding")
    public CommonResult<Boolean> commoditiesBatchGrounding(@RequestBody List<Integer> ids){

        List<Commodity> commodities = commodityService.findCommoditiesByIds(ids);
        for (Commodity commodity : commodities) {
            if (commodity.getGrounding() == 0){
                commodity.setGrounding(1);
                commodity.setGroundingTime(LocalDateTime.now());
            }
        }
        commodityService.updateBatchById(commodities);
        return CommonResult.success(true);
    }

    @ApiOperation("批量下架商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "商品id集合")
    })
    @PutMapping("/batchDown")
    public CommonResult<Boolean> commoditiesBatchDown(@RequestBody List<Integer> ids) {

        List<Commodity> commodities = commodityService.findCommoditiesByIds(ids);
        for (Commodity commodity : commodities) {
            if (commodity.getGrounding() == 1) {
                commodity.setGrounding(0);
                commodity.setDownTime(LocalDateTime.now());
            }
        }
        commodityService.updateBatchById(commodities);
        return CommonResult.success(true);
    }

}
